package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.service.IPatientService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-27.
 */
@Controller
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private IPatientService patientService;


    @RequestMapping("/loadPatient")
    @ResponseBody
    public Map<String, Object> loadPatient(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        String deptCode = request.getParameter("deptCode");
        if (StringUtils.hasLength(deptCode)) {
            res.put("deptCode", deptCode);
        }
        String month = request.getParameter("month");
        if (StringUtils.hasLength(month)) {
            res.put("month", month);
        }
        List<Map<String, Object>> data = patientService.load(res);
        res.put("root", data);
        return res;
    }


    @RequestMapping("/eval")
    @ResponseBody
    public Map<String, Object> eval(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String patientId = request.getParameter("patientId");
            res.put("patientId", patientId);
            patientService.eval(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }


    @RequestMapping("/fillQuestions")
    @ResponseBody
    public Map<String, Object> fillQuestions(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap<>();
        try {
            String qsStr = "question_";
            String mkStr = "mark_";
            String patientId = request.getParameter("visitCode");
            String deptCode = request.getParameter("deptCode");
            String[] questionIds = request.getParameterValues("questionId");
            List<Map<String, Object>> datas = new ArrayList<>();
            if (questionIds == null || questionIds.length == 0) {
                throw new RuntimeException("问卷数据有误，请核对再提交!");
            } else {
                for (String questionId : questionIds) {
                    String qVal = request.getParameter(qsStr + questionId);
                    if (!StringUtils.hasLength(qVal)) {
                        throw new RuntimeException("提示：您还有题目未选择满意度!");
                    } else {
                        Map<String, Object> data = new HashMap<>();
                        data.put("questionId", questionId);
                        data.put("grade", qVal);
                        data.put("mark", request.getParameter(mkStr + questionId));
                        datas.add(data);
                    }
                }
            }
            res.put("detail", datas);
            res.put("patientId", patientId);
            res.put("targetDeptCode", deptCode);
            patientService.fillQuestions(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
}
